package org.skyui.controllers;

import java.io.Serializable;

import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.AbstractBean;
import org.skyui.beans.OrganizationBean;
import org.skyui.config.BeansTableViewConfig;
import org.skyui.core.OrganizationSetup;
import org.skyui.core.UIApplication;
import org.skyui.views.BeanCreateView;
import org.skyui.views.LoginWindow;

import com.baycrypt.server.beans.people.UserBean;
import com.baycrypt.server.setup.CafeSetup;
import com.baycrypt.vaadinui.VaadinUIApplication;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.LoginForm.LoginListener;
import com.vaadin.ui.Window.Notification;

public class LoginController implements Serializable {

    private String productEdition;

    private String signInMessage;

    private OrganizationSetup organizationSetup;

    public OrganizationSetup getOrganizationSetup() {
	return organizationSetup;
    }

    public void setOrganizationSetup(OrganizationSetup organizationSetup) {
	this.organizationSetup = organizationSetup;
    }

    /**
     * @param
     * 
     */

    public LoginController(String productEdition, String signInMessage,
	    String registerMessage, boolean allowFacebookLogin,
	    boolean allowLocalLogin, OrganizationSetup setup) {
	this.productEdition = productEdition;
	this.signInMessage = signInMessage;
	this.organizationSetup = setup;
    }

    public LoginController(String productEdition, String signInMessage,
	    String registerMessage, boolean allowFacebookLogin,
	    boolean allowLocalLogin) {
	this(productEdition, signInMessage, registerMessage,
		allowFacebookLogin, allowLocalLogin, null);
    }

    public String getSignInMessage() {
	return signInMessage;
    }

    public void setSignInMessage(String signInMessage) {
	this.signInMessage = signInMessage;
    }

    public LoginController() {
	this("default", "Please Log In", "Dont have an account?", true, true);
    }

    public String getProductEdition() {
	return productEdition;
    }

    private static final long serialVersionUID = 6925108073720397644L;

    private LoginWindow loginWindow;
    private LoginWindow captiveLoginWindow;

    public LoginWindow createLoginWindow(String name) {
	this.loginWindow = new LoginWindow(name, this, false);
	return this.loginWindow;
    }

    public LoginWindow createCoovaLoginWindow() {
	this.captiveLoginWindow = new LoginWindow("captive", this, true);
	return this.captiveLoginWindow;
    }

    public LoginListener getListener() {
	return new LoginListener() {
	    /**
			 * 
			 */
	    private static final long serialVersionUID = 2888997317213042002L;

	    public void onLogin(LoginEvent event) {
		VaadinUIApplication.getInstance().setProductEdition(
			getProductEdition());
		String username = event.getLoginParameter("username");
		String password = event.getLoginParameter("password");
		try {
		    ((UIApplication) VaadinUIApplication.getInstance())
			    .authenticate(username, password,
				    getProductEdition());
		    VaadinUIApplication.getInstance().setLogoutURL(
			    LoginController.this.loginWindow.getURL().toString());
		    LoginController.this.loginWindow.open(new ExternalResource(
			    VaadinUIApplication.getInstance().getURL()));
		} catch (Exception e) {
		    if (!(e instanceof IllegalArgumentException))
			e.printStackTrace();
		    LoginController.this.loginWindow.showNotification(
			    e.getMessage(),
			    Notification.TYPE_ERROR_MESSAGE);
		}
	    }

	};
    }

    public ClickListener getUserRegistrationListener() {

	return new ClickListener() {

	    /**
			 * 
			 */
	    private static final long serialVersionUID = 6341936371477798774L;

	    public void buttonClick(ClickEvent event) {

		VaadinUIApplication.getInstance().setProductEdition(
			getProductEdition());

		final Window subwindow;
		BeanSetManager globalUserManager = BeanSetManager.getManager(
			UserBean.class, null);

		subwindow = new Window(
			BeansTableViewConfig
				.getAddItemCaption(globalUserManager));
		VerticalLayout layout = (VerticalLayout) subwindow.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
		// layout.setSizeFull();
		UserBean bean = (UserBean) globalUserManager
			.createTemplate();
		subwindow.setWidth("600px");
		AbstractBean bi = globalUserManager.createTemplate();
		BeanCreateView st = new BeanCreateView("", "", bi,
			VaadinUIApplication.getInstance(), null,
			globalUserManager);
		subwindow.addComponent(st);

		LoginController.this.loginWindow.getWindow().addWindow(
			subwindow);
		subwindow.center();

	    }
	};
    }

    public ClickListener getOrganizationRegistrationListener() {

	return new ClickListener() {

	    /**
			 * 
			 */
	    private static final long serialVersionUID = 6341936371477798774L;

	    public void buttonClick(ClickEvent event) {

		final Window subwindow;
		BeanSetManager manager = BeanSetManager.getManager(
			OrganizationBean.class, null);
		VaadinUIApplication.getInstance().setProductEdition(
			getProductEdition());
		subwindow = new Window(
			BeansTableViewConfig.getAddItemCaption(manager));
		VerticalLayout layout = (VerticalLayout) subwindow.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
		subwindow.setWidth("600px");
		subwindow.setScrollable(true);
		OrganizationBean bean = (OrganizationBean) manager
			.createTemplate();
		bean.setProductEdition(getProductEdition());

		bean.setOrganizationSetup(getOrganizationSetup());

		BeanCreateView st = new BeanCreateView("", "", bean,
			VaadinUIApplication.getInstance(), null, manager);
		subwindow.addComponent(st);

		LoginController.this.loginWindow.getWindow().addWindow(
			subwindow);
		subwindow.center();

	    }
	};
    }

    public ClickListener getFacebookButtonListener(final String clientID,
	    final String redirectURL) {
	return new ClickListener() {

	    @Override
	    public void buttonClick(ClickEvent event) {
		VaadinUIApplication.getInstance().setProductEdition(
			getProductEdition());
		Window w = event.getButton().getWindow();
		w.open(new ExternalResource(
			"https://graph.facebook.com/oauth/authorize?"
				+ "client_id=" + clientID + "&redirect_uri="
				+ redirectURL + "&scope=email"));

	    }
	};
    }

}
